#!/bin/bash

echo "=== 测试销售机会API接口 ==="

# 设置token
TOKEN="Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJvYS5nb3VndWNtcy5jb20iLCJhdWQiOiJnb3VndW9hIiwiaWF0IjoxNzY0MDMzNDA1LCJleHAiOjE3NjQxMTk4MDUsInN1YiI6MSwiZGF0YSI6eyJ1c2VyaWQiOjF9fQ.LLgWZm7YDB6AtxDLMLxLVvDH_j_ea7n_8iAMcWgmI-M"

# 设置请求头
HEADERS="-H 'Content-Type: application/x-www-form-urlencoded' -H 'Authorization: $TOKEN'"

echo ""
echo "================================"
echo "1. 测试创建销售机会..."
echo "================================"
CREATE_RESPONSE=$(curl -s $HEADERS -X POST "http://127.0.0.1:8000/api/crm/opportunity/add" \
  -d 'customer_id=1&contact_id=1&discovery_date=2025-01-15&category=1&level=1&title=测试销售机会&description=这是一个测试机会&expected_sign_date=2025-02-01&expected_amount=50000&remarks=测试备注')

echo "创建销售机会响应:"
echo $CREATE_RESPONSE | python3 -m json.tool

# 提取机会ID
OPPORTUNITY_ID=$(echo $CREATE_RESPONSE | grep -o '"id":[0-9]*' | cut -d':' -f2)
echo "机会ID: $OPPORTUNITY_ID"

if [ -n "$OPPORTUNITY_ID" ] && [ "$OPPORTUNITY_ID" != "null" ]; then
  echo ""
  echo "================================"
  echo "2. 测试获取销售机会详情..."
  echo "================================"
  curl -s $HEADERS "http://127.0.0.1:8000/api/crm/opportunity/detail?id=$OPPORTUNITY_ID" | python3 -m json.tool

  echo ""
  echo "================================"
  echo "3. 测试编辑销售机会..."
  echo "================================"
  EDIT_RESPONSE=$(curl -s $HEADERS -X POST "http://127.0.0.1:8000/api/crm/opportunity/edit" \
    -d "id=$OPPORTUNITY_ID&title=编辑后的销售机会&description=编辑后的描述&category=2&level=2")

  echo "编辑销售机会响应:"
  echo $EDIT_RESPONSE | python3 -m json.tool

  echo ""
  echo "================================"
  echo "4. 测试获取销售机会列表..."
  echo "================================"
  curl -s $HEADERS "http://127.0.0.1:8000/api/crm/opportunity/list?page=1&limit=10" | python3 -m json.tool
fi

echo ""
echo "=== 测试完成 ==="
